package com.dxd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.dxd.entity.MessageType;
import com.dxd.entity.PageEntity;

public class MessageTypeDao extends PublicDao<MessageType> {

	@Override
	protected MessageType result(ResultSet rs) throws SQLException {
		MessageType messageType = new MessageType();
		while (rs.next()) {
			messageType.setId(rs.getInt("id"));
			messageType.setName(rs.getString("name"));
			MessageType parentmessageType = new MessageType();
			parentmessageType.setId(rs.getInt("p_id"));
			parentmessageType.setName(rs.getString("p_name"));
			parentmessageType.setType(rs.getInt("p_type"));
			parentmessageType.setCreateTime(rs.getString("p_create_time"));
			messageType.setType(rs.getInt("type"));
			messageType.setParent(parentmessageType);
			messageType.setCreateTime(rs.getString("create_time"));
		}
		return messageType;
	}

	@Override
	protected List<MessageType> results(ResultSet rs) throws SQLException {
		List<MessageType> messageTypes = new ArrayList<MessageType>();
		while (rs.next()) {
			MessageType messageType = new MessageType();
			messageType.setId(rs.getInt("id"));
			messageType.setName(rs.getString("name"));
			MessageType parentmessageType = new MessageType();
			parentmessageType.setId(rs.getInt("p_id"));
			parentmessageType.setName(rs.getString("p_name"));
			parentmessageType.setType(rs.getInt("p_type"));
			parentmessageType.setCreateTime(rs.getString("p_create_time"));
			messageType.setType(rs.getInt("type"));
			messageType.setParent(parentmessageType);
			messageType.setCreateTime(rs.getString("create_time"));
			messageTypes.add(messageType);
			messageType = null;
		}
		return messageTypes;
	}

	/**
	 * 查询类型，带分页
	 * 
	 * @param page
	 * @param size
	 * @param name
	 * @param type
	 * @return
	 */
	public PageEntity<MessageType> queryList(int page, int size, String name, Integer type) {
		PageEntity<MessageType> pageEntity = new PageEntity<MessageType>();
		StringBuffer sql = new StringBuffer(
				"SELECT a.*,b.id AS p_id,b.name as p_name,b.type as p_type,b.create_time as p_create_time FROM message_type a LEFT JOIN message_type b ON a.parent_id=b.id where a.type=2 ");
		StringBuffer countsql = new StringBuffer("SELECT COUNT(1) as count FROM message_type where type=2 ");
		if (name != null && !"".equals(name)) {
			sql.append(" and a.name like'%");
			sql.append(name);
			sql.append("%'");
			countsql.append(" and name like'%");
			countsql.append(name);
			countsql.append("%'");

		}
		if (type != null) {
			sql.append(" and a.parent_id= ");
			sql.append(type);
			countsql.append(" and parent_id= ");
			countsql.append(type);
		}
		int count = queryCount(countsql.toString(), null);
		sql.append(" LIMIT ");
		sql.append((page - 1) * size);
		sql.append(",");
		sql.append(size);
		List<MessageType> list = queryList(sql.toString(), null);
		pageEntity.setPage(page);
		pageEntity.setSize(size);
		pageEntity.setList(list);
		pageEntity.setTotalCount(count);
		return pageEntity;
	}

	public List<MessageType> queryList(Integer type) {
		StringBuffer sql = new StringBuffer(
				"SELECT a.*,b.id AS p_id,b.name as p_name,b.type as p_type,b.create_time as p_create_time FROM message_type a LEFT JOIN message_type b ON a.parent_id=b.id where a.type=2 ");
		if (type != null) {
			sql.append(" and a.parent_id= ");
			sql.append(type);
		}
		List<MessageType> list = queryList(sql.toString(), null);
		return list;
	}

	/**
	 * 添加类型
	 * 
	 * @param messageType
	 * @return
	 */
	public int addMessageType(MessageType messageType) {
		String sql = "INSERT INTO message_type(name,parent_id,type,create_time) VALUES(?,?,?,?)";
		List<Object> objs = new ArrayList<Object>();
		objs.add(messageType.getName());
		objs.add(messageType.getParent().getId());
		objs.add(messageType.getType());
		objs.add(messageType.getCreateTime());
		return AddOrRemoveOrUpdate(sql, objs);
	}

	/**
	 * 修改
	 * 
	 * @param messageType
	 * @return
	 */
	public int updateMessageType(MessageType messageType) {
		String sql = "UPDATE message_type SET name=?,parent_id=? WHERE id=?";
		List<Object> objs = new ArrayList<Object>();
		objs.add(messageType.getName());
		objs.add(messageType.getParent().getId());
		objs.add(messageType.getId());
		return AddOrRemoveOrUpdate(sql, objs);
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	public int deleteByUserIds(String ids) {
		StringBuffer sql = new StringBuffer("DELETE FROM message_type WHERE id in(");
		sql.append(ids);
		sql.append(")");
		return AddOrRemoveOrUpdate(sql.toString(), null);
	}

	/**
	 * 根据名称查询
	 * 
	 * @param name
	 * @return
	 */
	public MessageType queryByName(String name, String parentId) {
		List<Object> objs = new ArrayList<Object>();
		objs.add(name);
		objs.add(parentId);
		String sql = "SELECT a.*,b.id AS p_id,b.name as p_name,b.type as p_type,b.create_time as p_create_time FROM message_type a LEFT JOIN message_type b ON a.parent_id=b.id where a.name=? and a.type=2 and b.id=?";
		return queryOne(sql, objs);
	}

	/**
	 * 查询所有的分类
	 * 
	 * @return
	 */
	public List<MessageType> queryList() {
		StringBuffer sql = new StringBuffer(
				"SELECT a.*,b.id AS p_id,b.name as p_name,b.type as p_type,b.create_time as p_create_time FROM message_type a LEFT JOIN message_type b ON a.parent_id=b.id where a.type=2 group by a.name ");
		List<MessageType> list = queryList(sql.toString(), null);
		return list;
	};

}
